<template>
  <div class="createPost-container" style="margin:20px">
    <el-form v-if="!infoLoading" ref="postForm" :model="postForm" class="form-container" label-width="100px">
      <el-form-item label="便民服务" prop="content" style="margin-top:0px">
        <el-input v-model="postForm.content" style="width:800px;clear:both;margin-top:0px" rows="20" type="textarea" />
      </el-form-item>
      <el-form-item style="margin:0">
        <el-button type="success" :loading="loading" @click="submitForm">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

const defaultForm = {
  content: '',
  id: null
}
export default {
  name: 'ArticleDetail',
  props: {
  },
  data() {
    return {
      infoid: null,
      infoLoading: false,
      postForm: Object.assign({}, defaultForm),
      loading: false
    }
  },
  watch: {
  },
  mounted() {
    this.infoid = 1
    this.myHeaders = {
      Authorization: 'Bearer ' + localStorage.getItem('token')
    }
    this.getData(this.infoid)
  },
  created() {
    // Why need to make a copy of this.$route here?
    // Because if you enter this page and quickly switch tag, may be in the execution of the setTagsViewTitle function, this.$route is no longer pointing to the current page
    // https://github.com/PanJiaChen/vue-element-admin/issues/1221
    this.tempRoute = Object.assign({}, this.$route)
  },
  methods: {
    getData(id) {
      this.postForm = Object.assign({}, defaultForm)
      this.infoLoading = true
      if (id) {
        this.$http
          .get('/api/busContent/getById?id=' + id)
          .then(response => {
            this.infoLoading = false
            if (response.data.success === true) {
              this.postForm.content = response.data.result
            } else {
              console.log('获取失败:' + response.data.message)
              this.$message({
                message: '获取失败:' + response.data.message,
                type: 'error'
              })
            }
          })
          .catch(error => {
            console.log(error)
            this.infoLoading = false
          })
      }
    },
    submitForm() {
      this.$refs.postForm.validate(valid => {
        if (valid) {
          const para = {
            content: this.postForm.content,
            id: this.infoid
          }
          this.loading = true
          this.$http
            .post('/api/busContent/save', para)
            .then(response => {
              this.loading = false
              if (response.data.success === true) {
                this.$message({
                  message: '提交成功',
                  type: 'success'
                })
                this.postForm = Object.assign({}, defaultForm)
                this.getData(this.infoid)
                this.loading = false
              } else {
                console.log('提交失败:' + response.data.message)
                this.$message({
                  message: '提交失败:' + response.data.message,
                  type: 'error'
                })
              }
            })
            .catch(function(error) {
              console.log(error)
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import "~@/styles/mixin.scss";

.createPost-container {
  position: relative;

  .createPost-main-container {
    // padding: 40px 45px 20px 50px;

    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;

      .postInfo-container-item {
        float: left;
      }
    }
  }

  .word-counter {
    width: 40px;
    position: absolute;
    right: 10px;
    top: 0px;
  }
}

.article-textarea ::v-deep {
  textarea {
    padding-right: 40px;
    resize: none;
    border: none;
    border-radius: 0px;
    border-bottom: 1px solid #bfcbd9;
  }
}
</style>
